package com.ruoyi.tfjData.utils;

import com.ruoyi.common.utils.BigDecimalUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tfjData.domain.plat.DataTfj;

import java.math.RoundingMode;

/**
 * 标况参数计算工具类
 */
public class BiaokuangUtils {

    /**
     * 风量计算
     * 转速转换系数   * 风量
     *
     * @return
     */
    public static String fengLiangFunc(DataTfj dataTfjModify) {
        String zhszhhxsh = dataTfjModify.getZhszhhxsh() == null ? "" : dataTfjModify.getZhszhhxsh();
        if (StringUtils.isEmpty(zhszhhxsh)) {
            zhszhhxsh = zhuanSuZhuanHuanXSFunc(dataTfjModify.getEdzhs(), dataTfjModify.getSczhs());
            dataTfjModify.setZhszhhxsh(zhszhhxsh);
        }
        if (StringUtils.isEmpty(dataTfjModify.getFl()) || StringUtils.isEmpty(zhszhhxsh)) {
            return "";
        }
        return BigDecimalUtils.mul(dataTfjModify.getFl(), zhszhhxsh).setScale(1, RoundingMode.HALF_UP).toString();
    }

    /**
     * 轴功率1 kW
     * 空气密度转换系数 * 转速转换系数 ³ *  轴功率1
     *
     * @return
     */
    public static String zhouGonglv1Func(DataTfj dataTfjModify) {
        String zhszhhxsh = dataTfjModify.getZhszhhxsh() == null ? "" : dataTfjModify.getZhszhhxsh();
        if (StringUtils.isEmpty(zhszhhxsh)) {
            zhszhhxsh = zhuanSuZhuanHuanXSFunc(dataTfjModify.getEdzhs(), dataTfjModify.getSczhs());
            dataTfjModify.setZhszhhxsh(zhszhhxsh);
        }
        String kqmdzhhxsh = dataTfjModify.getKqmdzhhxsh() == null ? "" : dataTfjModify.getKqmdzhhxsh();
        if (StringUtils.isEmpty(kqmdzhhxsh)) {
            kqmdzhhxsh = kongQiMiDuZhuanHuanXSFunc(dataTfjModify.getKqmd());
            dataTfjModify.setKqmdzhhxsh(kqmdzhhxsh);
        }
        if (StringUtils.isEmpty(dataTfjModify.getZhszhhxsh()) || StringUtils.isEmpty(dataTfjModify.getKqmdzhhxsh()) || StringUtils.isEmpty(dataTfjModify.getDj1Zhgl())) {
            return "";
        }
        String param = BigDecimalUtils.mul(BigDecimalUtils.mul(dataTfjModify.getZhszhhxsh(), dataTfjModify.getZhszhhxsh()).toString(), dataTfjModify.getZhszhhxsh()).toString();
        return BigDecimalUtils.mul( BigDecimalUtils.mul(dataTfjModify.getKqmdzhhxsh(), dataTfjModify.getDj1Zhgl()).toString(), param).setScale(2, RoundingMode.HALF_UP).toString();
    }

    /**
     * 轴功率2 kW
     * 空气密度转换系数 * 转速转换系数 ³ *  轴功率2
     *
     * @return
     */
    public static String zhouGonglv2Func(DataTfj dataTfjModify) {
        String zhszhhxsh = dataTfjModify.getZhszhhxsh() == null ? "" : dataTfjModify.getZhszhhxsh();
        if (StringUtils.isEmpty(zhszhhxsh)) {
            zhszhhxsh = zhuanSuZhuanHuanXSFunc(dataTfjModify.getEdzhs(), dataTfjModify.getSczhs());
            dataTfjModify.setZhszhhxsh(zhszhhxsh);
        }

        String kqmdzhhxsh = dataTfjModify.getKqmdzhhxsh() == null ? "" : dataTfjModify.getKqmdzhhxsh();
        if (StringUtils.isEmpty(kqmdzhhxsh)) {
            kqmdzhhxsh = kongQiMiDuZhuanHuanXSFunc(dataTfjModify.getKqmd());
            dataTfjModify.setKqmdzhhxsh(kqmdzhhxsh);
        }
        if (StringUtils.isEmpty(zhszhhxsh) || StringUtils.isEmpty(kqmdzhhxsh) || StringUtils.isEmpty(dataTfjModify.getDj2Zhgl())) {
            return "";
        }
        String param = BigDecimalUtils.mul(BigDecimalUtils.mul(zhszhhxsh, zhszhhxsh).toString(), zhszhhxsh).toString();
        return BigDecimalUtils.mul(
                BigDecimalUtils.mul(kqmdzhhxsh, dataTfjModify.getDj2Zhgl()).toString(),
                param).setScale(2, RoundingMode.HALF_UP).toString();
    }

    /**
     * 风机装置静压 Pa
     * 空气密度转换系数 * 转速转换系数 ² * 风机装置静压
     *
     * @return
     */
    public static String fengJiZhuangZhiJYFunc(DataTfj dataTfjModify) {
        String zhszhhxsh = dataTfjModify.getZhszhhxsh() == null ? "" : dataTfjModify.getZhszhhxsh();
        if (StringUtils.isEmpty(zhszhhxsh)) {
            zhszhhxsh = zhuanSuZhuanHuanXSFunc(dataTfjModify.getEdzhs(), dataTfjModify.getSczhs());
            dataTfjModify.setZhszhhxsh(zhszhhxsh);
        }
        String kqmdzhhxsh = dataTfjModify.getKqmdzhhxsh() == null ? "" : dataTfjModify.getKqmdzhhxsh();
        if (StringUtils.isEmpty(kqmdzhhxsh)) {
            kqmdzhhxsh = kongQiMiDuZhuanHuanXSFunc(dataTfjModify.getKqmd());
            dataTfjModify.setKqmdzhhxsh(kqmdzhhxsh);
        }
        if (StringUtils.isEmpty(zhszhhxsh)
                || StringUtils.isEmpty(kqmdzhhxsh)
                || StringUtils.isEmpty(dataTfjModify.getFjjy())) {
            return "";
        }
        String param = BigDecimalUtils.mul(zhszhhxsh, zhszhhxsh).toString();
        return BigDecimalUtils.mul(
                BigDecimalUtils.mul(kqmdzhhxsh, dataTfjModify.getFjjy()).toString(),
                param).setScale(2, RoundingMode.HALF_UP).toString();
    }

    /**
     * 风机装置全压 Pa
     * 空气密度转换系数 * 转速转换系数 ²  * 风机装置全压
     *
     * @return
     */
    public static String fengJiZhuangZhiQYFunc(DataTfj dataTfjModify) {

        String zhszhhxsh = dataTfjModify.getZhszhhxsh() == null ? "" : dataTfjModify.getZhszhhxsh();
        if (StringUtils.isEmpty(zhszhhxsh)) {
            zhszhhxsh = zhuanSuZhuanHuanXSFunc(dataTfjModify.getEdzhs(), dataTfjModify.getSczhs());
            dataTfjModify.setZhszhhxsh(zhszhhxsh);
        }

        String kqmdzhhxsh = dataTfjModify.getKqmdzhhxsh() == null ? "" : dataTfjModify.getKqmdzhhxsh();
        if (StringUtils.isEmpty(kqmdzhhxsh)) {
            kqmdzhhxsh = kongQiMiDuZhuanHuanXSFunc(dataTfjModify.getKqmd());
            dataTfjModify.setKqmdzhhxsh(kqmdzhhxsh);
        }
        if (StringUtils.isEmpty(zhszhhxsh)
                || StringUtils.isEmpty(kqmdzhhxsh)
                || StringUtils.isEmpty(dataTfjModify.getFjqy())) {
            return "";
        }
        String param = BigDecimalUtils.mul(zhszhhxsh, zhszhhxsh).toString();
        return BigDecimalUtils.mul(
                BigDecimalUtils.mul(kqmdzhhxsh, dataTfjModify.getFjqy()).toString(),
                param).setScale(2, RoundingMode.HALF_UP).toString();
    }

    /**
     * 风机装置静压功率 kW
     * 空气密度转换系数 * 转速转换系数 ³  * 风机装置静压功率
     *
     * @return
     */
    public static String fengJiZhuangZhiJYGLFunc(DataTfj dataTfjModify) {

        String zhszhhxsh = dataTfjModify.getZhszhhxsh() == null ? "" : dataTfjModify.getZhszhhxsh();
        if (StringUtils.isEmpty(zhszhhxsh)) {
            zhszhhxsh = zhuanSuZhuanHuanXSFunc(dataTfjModify.getEdzhs(), dataTfjModify.getSczhs());
            dataTfjModify.setZhszhhxsh(zhszhhxsh);
        }

        String kqmdzhhxsh = dataTfjModify.getKqmdzhhxsh() == null ? "" : dataTfjModify.getKqmdzhhxsh();
        if (StringUtils.isEmpty(kqmdzhhxsh)) {
            kqmdzhhxsh = kongQiMiDuZhuanHuanXSFunc(dataTfjModify.getKqmd());
            dataTfjModify.setKqmdzhhxsh(kqmdzhhxsh);
        }
        if (StringUtils.isEmpty(zhszhhxsh)
                || StringUtils.isEmpty(kqmdzhhxsh)
                || StringUtils.isEmpty(dataTfjModify.getFjjygl())) {
            return "";
        }
        String param = BigDecimalUtils.mul(BigDecimalUtils.mul(zhszhhxsh, zhszhhxsh).toString(), zhszhhxsh).toString();
        return BigDecimalUtils.mul(
                BigDecimalUtils.mul(kqmdzhhxsh, dataTfjModify.getFjjygl()).toString(),
                param).setScale(2, RoundingMode.HALF_UP).toString();
    }

    /**
     * 风机装置全压功率 kW
     * 空气密度转换系数 * 转速转换系数 ³ *  风机装置全压功率
     *
     * @return
     */
    public static String fengJiZhuangZhiQYGLFunc(DataTfj dataTfjModify) {
        String zhszhhxsh = dataTfjModify.getZhszhhxsh() == null ? "" : dataTfjModify.getZhszhhxsh();
        if (StringUtils.isEmpty(zhszhhxsh)) {
            zhszhhxsh = zhuanSuZhuanHuanXSFunc(dataTfjModify.getEdzhs(), dataTfjModify.getSczhs());
            dataTfjModify.setZhszhhxsh(zhszhhxsh);
        }

        String kqmdzhhxsh = dataTfjModify.getKqmdzhhxsh() == null ? "" : dataTfjModify.getKqmdzhhxsh();
        if (StringUtils.isEmpty(kqmdzhhxsh)) {
            kqmdzhhxsh = kongQiMiDuZhuanHuanXSFunc(dataTfjModify.getKqmd());
            dataTfjModify.setKqmdzhhxsh(kqmdzhhxsh);
        }
        if (StringUtils.isEmpty(dataTfjModify.getZhszhhxsh()) || StringUtils.isEmpty(dataTfjModify.getKqmdzhhxsh()) || StringUtils.isEmpty(dataTfjModify.getFjqygl())) {
            return "";
        }
        String param = BigDecimalUtils.mul(BigDecimalUtils.mul(dataTfjModify.getZhszhhxsh(), dataTfjModify.getZhszhhxsh()).toString(), dataTfjModify.getZhszhhxsh()).toString();
        return BigDecimalUtils.mul(
                BigDecimalUtils.mul(dataTfjModify.getKqmdzhhxsh(), dataTfjModify.getFjqygl()).toString(),
                param).setScale(2, RoundingMode.HALF_UP).toString();
    }

    /**
     * 风机装置静压效率
     * 标况风机装置静压功率 / ( 标况轴功率1 + 标况轴功率2 )
     *
     * @return
     */
    public static String fengJiZhuangZhiJYXLFunc(DataTfj dataTfjModify) {
        if (StringUtils.isEmpty(dataTfjModify.getBkDj1Zhgl())) {
            dataTfjModify.setBkDj1Zhgl(zhouGonglv1Func(dataTfjModify));
        }
        if (StringUtils.isEmpty(dataTfjModify.getBkDj2Zhgl())) {
            dataTfjModify.setBkDj2Zhgl(zhouGonglv2Func(dataTfjModify));
        }
        if ((StringUtils.isEmpty(dataTfjModify.getBkDj1Zhgl())
                && StringUtils.isEmpty(dataTfjModify.getBkDj2Zhgl()))
                || StringUtils.isEmpty(dataTfjModify.getBkFjjygl())) {
            return "";
        }
        String dj1Zhgl = "0";
        String dj2Zhgl = "0";
        if (!StringUtils.isEmpty(dataTfjModify.getBkDj1Zhgl())) {
            dj1Zhgl = dataTfjModify.getBkDj1Zhgl();
        }
        if (!StringUtils.isEmpty(dataTfjModify.getBkDj2Zhgl())) {
            dj2Zhgl = dataTfjModify.getBkDj2Zhgl();
        }
        String fenMu = BigDecimalUtils.add(dj1Zhgl, dj2Zhgl).toString();
        if (BigDecimalUtils.compareTo(fenMu, "0") == 0) {
            return "";
        }
        return percentJudge(BigDecimalUtils.div(dataTfjModify.getBkFjjygl(), fenMu, 4).toString());
    }

    /**
     * 风机装置全压效率
     * 标况风机装置全压功率 / ( 标况轴功率1 + 标况轴功率2 )
     *
     * @return
     */
    public static String fengJiZhuangZhiQYXLFunc(DataTfj dataTfjModify) {
        if (StringUtils.isEmpty(dataTfjModify.getBkDj1Zhgl())) {
            dataTfjModify.setBkDj1Zhgl(zhouGonglv1Func(dataTfjModify));
        }
        if (StringUtils.isEmpty(dataTfjModify.getBkDj2Zhgl())) {
            dataTfjModify.setBkDj2Zhgl(zhouGonglv2Func(dataTfjModify));
        }
        if ((StringUtils.isEmpty(dataTfjModify.getBkDj1Zhgl()) && StringUtils.isEmpty(dataTfjModify.getBkDj2Zhgl())) || StringUtils.isEmpty(dataTfjModify.getBkFjqygl())) {
            return "";
        }
        String dj1Zhgl = "0";
        String dj2Zhgl = "0";
        if (!StringUtils.isEmpty(dataTfjModify.getBkDj1Zhgl())) {
            dj1Zhgl = dataTfjModify.getBkDj1Zhgl();
        }
        if (!StringUtils.isEmpty(dataTfjModify.getBkDj2Zhgl())) {
            dj2Zhgl = dataTfjModify.getBkDj2Zhgl();
        }
        String fenMu = BigDecimalUtils.add(dj1Zhgl, dj2Zhgl).toString();
        if (BigDecimalUtils.compareTo(fenMu, "0") == 0) {
            return "";
        }
        return percentJudge(BigDecimalUtils.div(dataTfjModify.getBkFjqygl(), fenMu, 4).toString());
    }

    /**
     * 效率类小于0则返回0 大于1则返回1
     *
     * @param value
     * @return
     */
    private static String percentJudge(String value) {
        if (BigDecimalUtils.compareTo(value.toString(), "0") == -1) {
            return "0.00";
        }
        if (BigDecimalUtils.compareTo(value.toString(), "1") == 1) {
            return "1.00";
        }
        return value;
    }

    /**
     * 计算转速转换系数
     * 额定转速 / 实测转速
     */
    public static String zhuanSuZhuanHuanXSFunc(String edzhs, String sczhs) {
        if (StringUtils.isEmpty(edzhs)) {
            return "";
        }
        if (BigDecimalUtils.compareTo(sczhs, "0") == 0) {
            return "";
        }
        return BigDecimalUtils.div(edzhs, sczhs, 4).toString();
    }

    /**
     * 空气密度转换系数
     * 标准空气密度(1.2kg/m³) / 实测空气密度
     *
     * @return
     */
    public static String kongQiMiDuZhuanHuanXSFunc(String kqmd) {
        if (StringUtils.isEmpty(kqmd)
                || BigDecimalUtils.compareTo(kqmd, "0") == 0) {
            return "";
        }
        return BigDecimalUtils.div("1.2", kqmd, 4).toString();
    }

}
